#!/usr/bin/env python3


from subprocess import call
import sys
def buildLM(args):
    """
    build LM with args from stdin
    """

    print("======>>"+"set release CS")
    try:
        retcode = call("/proj/CoolTools/bin/cs_init" + " -t" + " rel", shell=True)
        if retcode < 0:
            print("Child was terminated by signal", -retcode, file=sys.stderr)
        else:
            print("Child returned", retcode, file=sys.stderr)
    except OSError as e:
        print("Execution failed:", e, file=sys.stderr)


    print("======>>"+"add correct perl version")
    try:
        retcode = call("/app/modules/0/bin/modulecmd tcsh add " + "lsf", shell=True)
        #retcode = call("module add" + "lsf", shell=True)
        if retcode < 0:
            print("Child was terminated by signal", -retcode, file=sys.stderr)
        else:
            print("Child returned", retcode, file=sys.stderr)
    except OSError as e:
        print("Execution failed:", e, file=sys.stderr)


    try:
        retcode = call("/app/modules/0/bin/modulecmd tcsh add " + "perl/5.8.9.827", shell=True)
        if retcode < 0:
            print("Child was terminated by signal", -retcode, file=sys.stderr)
        else:
            print("Child returned", retcode, file=sys.stderr)
    except OSError as e:
        print("Execution failed:", e, file=sys.stderr)

    print("======>>"+"run lm_build.pl to build "+args.lm[0]+" on track:"+args.track[0]+" for hwgen:"+args.hwgen[0])
    targetLm =""
    if args.lm[0] == 'dch':
        targetLm = 'dchsr'
    elif args.lm[0]== 'eul':
        targetLm= 'eulsr'
    elif args.lm[0] == 'hs':
        targetLm= 'hssr'
    elif args.lm[0] == 'ultch':
        targetLm= 'curie0'
    elif args.lm[0] == 'ulcch':
        targetLm= 'curie1'
    elif args.lm[0] == 'uldch':
        targetLm= 'faraday'
    elif args.lm == 'ul_1':
        targetLm[0] = 'ul_nyquist_1'
    elif args.lm == 'ul_2':
        targetLm[0] = 'ul_nyquist_2'
    elif args.lm == 'dl_1':
        targetLm = 'dl_nyquist_1'
    elif args.lm[0] == 'dl_2':
        targetLm = 'dl_nyquist_2'
    elif args.lm[0] == 'dl_3':
        targetLm = 'dl_nyquist_3'
    elif args.lm[0] == 'dl_4':
        targetLm = 'dl_nyquist_4'
    elif args.lm[0] == 'dl_5':
        targetLm = 'dl_nyquist_5'
    elif args.lm[0] == 'xl_1':
        targetLm = 'xl_nyquist_1'
    elif args.lm[0] == 'xl_m_1':
        targetLm = 'uldl_mercury_1'

    try:
        retcode = call("perl /vobs/rbs/hw/dpp_tools/subsystems/lm_build/lm_build.pl" + " --hwgen " + args.hwgen[0]+ " --target "+ targetLm + " --ranrel " + args.track[0], shell=True)
        if retcode < 0:
            print("Child was terminated by signal", -retcode, file=sys.stderr)
        else:
            print("Child returned", retcode, file=sys.stderr)
    except OSError as e:
        print("Execution failed:", e, file=sys.stderr)




def main():
    from argparse import ArgumentParser
    from argparse import ArgumentDefaultsHelpFormatter
    parser = ArgumentParser(\
            description = 'Build LM automatically for DUW.\
            available hwgen and track shall follow latest /vobs/rbs/bb/delivery/dp \
            content',\
            formatter_class = ArgumentDefaultsHelpFormatter\
            )
    parser.add_argument('-version', '-v',action = 'version', version = '%(prog)s 1.0')
    parser.add_argument('-hw', '--hwgen', nargs = 1, type = str, default = ['duw1'], dest = 'hwgen', choices = ['duw1','duw2'], help = 'Hardware Generation ')
    parser.add_argument('-t', '--track',nargs = 1, type = str, default = ['main'], dest = 'track', choices = ['main', 'w11a', 'w11b', 'w12a', 'w12b', 'w13a', 'w13b'], help = 'the product track for which you want to build LM' )
    lmTable ='''duw10,duw20,duw30:dch-->dch, eul-->eul, hs-->hs, ultch-->curie0, ulcch-->curie1, uldch-->faraday,
    duw11: uldch -->ul_1 && ul_2, ulcch-->ul_1, ultch-->ul_2, hs-->dl_1, dch-->dl_1, eul-->dl_1, xlcr-->xl_m_1
    duw31: uldch -->ul_1 && ul_2, ulcch-->ul_1, ultch-->ul_2 && xl_1, hs-->dl_3 && dl_4 && xl_1, dch-->dl_5 && xl_1, eul-->dl_4 && xl_1, xlcr-->xl_m_1
    duw41: uldch -->ul_1 && ul_2, ulcch-->ul_1, ultch-->ul_2, hs-->dl_3 && dl_4, dch-->dl_2, eul-->dl_4, xlcr-->xl_m_1
    duw01: uldch -->ul_1 && ul_2, ulcch-->ul_1, ultch-->xl_1, hs-->xl_1, dch-->xl_1, eul-->xl_1, xlcr-->xl_m_1'''
    parser.add_argument('-lm', nargs = 1, type = str, default = ['dch'], dest = 'lm', choices = ['dch', 'eul', 'hs', 'ultch', 'ulcch', 'uldch', 'ul_1', 'ul_2', 'dl_1', 'dl_2', 'dl_3', 'dl_4', 'dl_5', 'xl_1','xl_m_1'], help = lmTable )

    args = parser.parse_args()
    buildLM(args)



if __name__ == "__main__":
  main()
